package br.uefs.dexa.rfframework.db.annotation;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLImageCLEFAnnotationFormatter implements AnnotationFormatter {
	
	public static final int LINE_MAX_LENGTH = 60;
	
	public static final String ENCODING = "ISO-8859-1";

	@Override
	public String format(String textFilePath) 
	{
		return parseXML(textFilePath);
	}
	
	private String parseXML(String textFilePath)
	{
		
		BufferedReader br = null;
		
		//get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		Document dom = null;

		try {
			
			String line = "";
			String xml = "";
			 
			br = new BufferedReader(new InputStreamReader(new FileInputStream(textFilePath), ENCODING));
 
			while ((line = br.readLine()) != null) {
				xml += line + "\n";
			}
			
			br.close();

			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();

			InputSource is = new InputSource(new ByteArrayInputStream(xml.getBytes(ENCODING)));
			is.setEncoding (ENCODING);
			
			//parse using builder to get DOM representation of the XML file
			//dom = db.parse(textFilePath);
			dom = db.parse(is);


		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		
		}catch(SAXException se) {
			se.printStackTrace();
		
		}catch(IOException ioe) {
			ioe.printStackTrace();
			
		}catch(Exception e) {
			e.printStackTrace();
		
		} 
		
		
		return getformattedText(dom);
	}
	
	private String getformattedText(Document dom)
	{
		String text = "";
		
		//get the root element
		NodeList nodeList = dom.getDocumentElement().getChildNodes();
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			
			Node node = nodeList.item(i);

			switch(node.getNodeName()) {
			
			case "DOCNO":
				text +=  "<b>DOCNO: </b> " + node.getTextContent() + "<br>";break;
			case "TITLE":
				text +=  "<b>TITLE: </b>" + node.getTextContent() + "<br>";break;
			case "DESCRIPTION":
				text +=  breakInLines("<b>DESCRIPTION: </b>" + node.getTextContent()) + "<br>";break;
			case "NOTES":
				text +=  breakInLines("<b>NOTES: </b>" + node.getTextContent()) + "<br>";break;
			case "LOCATION":
				text +=  "<b>LOCATION: </b>" + node.getTextContent() + "<br>";break;
			case "DATE":
				text +=  "<b>DATE: </b>" + node.getTextContent() + "<br>";break;
			case "IMAGE":
				text +=  "<b>IMAGE: </b>" + node.getTextContent() + "<br>";break;
			case "THUMBNAIL":
				text +=  "<b>THUMBNAIL: </b>" + node.getTextContent() + "<br>";break;
			}

		}
		
		return text;
		 
	}
	
	private String breakInLines(String text)
	{
		String result = "";
		
		if (text.length() > LINE_MAX_LENGTH) 
		{
			int endIndex = LINE_MAX_LENGTH;
			
			while(text.toCharArray()[endIndex] != ' ' && endIndex < text.toCharArray().length - 1) {
				endIndex++;
			}
			
			result += text.substring(0, endIndex) + "<br>" + breakInLines(text.substring(endIndex, text.length()));
		}
		else
			result = text;
		
		return result;
	}
	

}
